import {
  getGame,addClick,addOpen,getConfig
} from "../../api/getApi"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myGame:[],
    banben:wx.getStorageSync('banben')?wx.getStorageSync('banben'):'1.0.0',
    userData:wx.getStorageSync('userData')?JSON.parse(wx.getStorageSync('userData')):{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    getConfig({page:1,limit:10}).then((res)=>{
      let banben = res.list.find(v=>v.type==="banben")
      let yinsi = res.list.find(v=>v.type==="yinsi")
      let shouquan = res.list.find(v=>v.type==="shouquan")
      if(banben){
        wx.setStorageSync('banben', banben.text)
      }
      if(yinsi){
        wx.setStorageSync('yinsi', yinsi.text)
      }
      if(shouquan){
        wx.setStorageSync('shouquan', shouquan.text)
      }
    })

  },
  goToMyGame(){
    wx.navigateTo({
      url: '/pages/myGame/myGame',
    })
  },
  goToGame(e){
    let itemdata = e.currentTarget.dataset.data
    if (itemdata.appid){
      wx.navigateToMiniProgram({
        appId: itemdata.appid, 
        path: '?wxgamepro=CpsCBgAAoXkpQY9UXS9__OwV',
        envVersion: 'release',
        success:(res)=>{
          let myGame = wx.getStorageSync('myGame')
          if(!myGame){
            myGame = []
          }else{
            myGame = JSON.parse(myGame).slice(0, 300)
          }
          if(!myGame.find(v=>v.id===itemdata.id)){
            myGame=[itemdata,...myGame]
          }
          wx.setStorageSync('myGame', JSON.stringify(myGame))
          this.setData({myGame:myGame})
          addOpen({id:itemdata.id})
        }
      })
      addClick({id:itemdata.id})
    }else{
      wx.showToast({
        title: `尊敬的大人对对不不起起！此游戏出了点小问题~`,
        icon: 'none',
      });
    }
  },
  goTo1(){
    wx.navigateTo({
      url: '/pages/yinsi/yinsi',
    })
  },
  goTo2(){
    wx.navigateTo({
      url: '/pages/shouquan/shouquan',
    })
  },
  goTo3(){
    wx.navigateTo({
      url: '/pages/myForum/myForum',
    })
  },
  goTo4(){
    wx.navigateTo({
      url: '/pages/myReplay/myReplay',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let myGame = wx.getStorageSync('myGame')
    if(!myGame){
      myGame = []
    }else{
      myGame = JSON.parse(myGame).slice(0, 20)
    }
    this.setData({myGame:myGame})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})